/*
 * @Author: 0x9DEFA478
 * @Date: 2021-09-06 20:36:49
 * @LastEditTime: 2021-09-07 18:00:51
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "H_FFT.h"
#include <math.h>


#define vH_FFT_PI_d	((double)3.141592653589793)


extern void H_FFT_ReverseData_Single(void* v,void* Src,int NumOfPoint);
extern void H_FFT_ReverseData_Double(void* v,void* Src,int NumOfPoint);


static void Init_W(H_FFT* h_fft){

  float *W_f;
  double *W_d;
  int i;

  if(h_fft->Type==vH_FFT_Type_Single){
  
    W_f=h_fft->W;

    for(i = 0;i<h_fft->NumOfPoint;i++){

      W_f[i*2]=(float)cos(-(double)i*2*vH_FFT_PI_d/(double)h_fft->NumOfPoint);
      W_f[i*2+1]=(float)sin(-(double)i*2*vH_FFT_PI_d/(double)h_fft->NumOfPoint);
    }

  }
  else if(h_fft->Type==vH_FFT_Type_Double){

    W_d=h_fft->W;

    for(i = 0;i<h_fft->NumOfPoint;i++){

      W_d[i*2]=cos(-(double)i*2*vH_FFT_PI_d/(double)h_fft->NumOfPoint);
      W_d[i*2+1]=sin(-(double)i*2*vH_FFT_PI_d/(double)h_fft->NumOfPoint);
    }
  }else{
  }
}

static void Init_BitReverseTable(H_FFT* h_fft){

  int re;
  int i;
  int bits;
  

  for(i=0;i<h_fft->NumOfPoint;i++){

    re=0;
    for(bits = 0;bits<h_fft->NumOfPointBits;bits++){
      re|=((i&(1<<bits))==0) ? 0 : (0x01<<(h_fft->NumOfPointBits-1-bits));
    }

    h_fft->BitReverseTable[i]=(unsigned short)re;
  }
}

/**
 * @brief 初始化h_fft
 * @param h_fft h_fft对象
 * @param NumOfPoint FFT点数
 * @param Type 源数据类型
 * @param BitReverseTable 用于存放位反转表的内存 长度:NumOfPoint*sizeof(unsigned short)
 * @param W 用于存放位反转表的内存 长度:NumOfPoint*2*(类型的字节宽度) 字节
 * @return 0:成功 其他:失败
 */
int H_FFT_Init(H_FFT* h_fft,int NumOfPoint,int Type,void* BitReverseTable,void* W){
  
  
  

  if(Type==vH_FFT_Type_Single){
    h_fft->ReverseData=H_FFT_ReverseData_Single;
  }else if(Type==vH_FFT_Type_Double){
    h_fft->ReverseData=H_FFT_ReverseData_Double;
  }else{
    return -1;
  }

  
  h_fft->Type=Type;
  h_fft->BitReverseTable=BitReverseTable;
  h_fft->W=W;
  h_fft->NumOfPoint=NumOfPoint;
  
  
  switch(h_fft->NumOfPoint){
    case 4:
      h_fft->NumOfPointBits=2;
      h_fft->Radix4_Enable=-1;
      break;
  
    case 8:
      h_fft->NumOfPointBits=3;
      h_fft->Radix4_Enable=0;
      break;
  
    case 16:
      h_fft->NumOfPointBits=4;
      h_fft->Radix4_Enable =-1;
      break;
  
    case 32:
      h_fft->NumOfPointBits=5;
      h_fft->Radix4_Enable=0;
      break;
  
    case 64:
      h_fft->NumOfPointBits=6;
      h_fft->Radix4_Enable =-1;
      break;
  
    case 128:
      h_fft->NumOfPointBits=7;
      h_fft->Radix4_Enable=0;
      break;
  
    case 256:
      h_fft->NumOfPointBits=8;
      h_fft->Radix4_Enable =-1;
      break;
  
    case 512:
      h_fft->NumOfPointBits=9;
      h_fft->Radix4_Enable=0;
      break;
  
    case 1024:
      h_fft->NumOfPointBits=10;
      h_fft->Radix4_Enable =-1;
      break;
  
    case 2048:
      h_fft->NumOfPointBits=11;
      h_fft->Radix4_Enable=0;
      break;
  
    case 4096:
      h_fft->NumOfPointBits=12;
      h_fft->Radix4_Enable =-1;
      break;
  
    case 8192:
      h_fft->NumOfPointBits=13;
      h_fft->Radix4_Enable=0;
      break;
  
    case 16384:
      h_fft->NumOfPointBits=14;
      h_fft->Radix4_Enable =-1;
      break;
  
    case 32768:
      h_fft->NumOfPointBits=15;
      h_fft->Radix4_Enable =0;
      break;
    case 65536:
      h_fft->NumOfPointBits=16;
      h_fft->Radix4_Enable =-1;
      break;
  
    default:
      return -1;
  }
  
  
  Init_W(h_fft);
  Init_BitReverseTable(h_fft);
  
  return 0;
}

/**
 * @brief 初始化h_fft 使用用户的反序算法
 * @param h_fft h_fft对象
 * @param NumOfPoint FFT点数
 * @param Type 源数据类型
 * @param ReverseData 反序方法
 * @param W 用于存放位反转表的内存 长度:NumOfPoint*2*(类型的字节宽度) 字节
 * @return 0:成功 其他:失败
 */
int H_FFT_UserReverse_Init(H_FFT* h_fft,int NumOfPoint,int Type,void (*ReverseData)(void*,void*,int),void* W){
  
  
  if(Type==vH_FFT_Type_Single){
    h_fft->ReverseData=ReverseData;
  }else if(Type==vH_FFT_Type_Double){
    h_fft->ReverseData=ReverseData;
  }else{
    return -1;
  }

  
  h_fft->Type=Type;
  h_fft->BitReverseTable=((void*)0);
  h_fft->W=W;
  h_fft->NumOfPoint=NumOfPoint;
  
  
  switch(h_fft->NumOfPoint){
    case 4:
      h_fft->NumOfPointBits=2;
      h_fft->Radix4_Enable=-1;
      break;
  
    case 8:
      h_fft->NumOfPointBits=3;
      h_fft->Radix4_Enable=0;
      break;
  
    case 16:
      h_fft->NumOfPointBits=4;
      h_fft->Radix4_Enable =-1;
      break;
  
    case 32:
      h_fft->NumOfPointBits=5;
      h_fft->Radix4_Enable=0;
      break;
  
    case 64:
      h_fft->NumOfPointBits=6;
      h_fft->Radix4_Enable =-1;
      break;
  
    case 128:
      h_fft->NumOfPointBits=7;
      h_fft->Radix4_Enable=0;
      break;
  
    case 256:
      h_fft->NumOfPointBits=8;
      h_fft->Radix4_Enable =-1;
      break;
  
    case 512:
      h_fft->NumOfPointBits=9;
      h_fft->Radix4_Enable=0;
      break;
  
    case 1024:
      h_fft->NumOfPointBits=10;
      h_fft->Radix4_Enable =-1;
      break;
  
    case 2048:
      h_fft->NumOfPointBits=11;
      h_fft->Radix4_Enable=0;
      break;
  
    case 4096:
      h_fft->NumOfPointBits=12;
      h_fft->Radix4_Enable =-1;
      break;
  
    case 8192:
      h_fft->NumOfPointBits=13;
      h_fft->Radix4_Enable=0;
      break;
  
    case 16384:
      h_fft->NumOfPointBits=14;
      h_fft->Radix4_Enable =-1;
      break;
  
    case 32768:
      h_fft->NumOfPointBits=15;
      h_fft->Radix4_Enable =0;
      break;
    case 65536:
      h_fft->NumOfPointBits=16;
      h_fft->Radix4_Enable =-1;
      break;
  
    default:
      return -1;
  }
    
  Init_W(h_fft);
  
  return 0;
}






